package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    void insert(Orders orders);


    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 查询历史订单
     * @param dto
     * @return
     */
    Page<Orders> page(OrdersPageQueryDTO dto);

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);
    @Select("select count(*) from orders where status = #{confirmed}")
    Integer getNumbersStatus(Integer confirmed);

    /**
     * 查询超时订单
     */
    @Select("select * from orders where status=#{integer} and order_time<#{time}")
    List<Orders> getByStatusAndOrderTime(Integer integer, LocalDateTime time);

    /**
     * 营业额统计
     * @param begintime
     * @param endtime
     * @param completed
     * @return
     */
    @Select("select sum(amount) from orders where order_time between #{begintime} and #{endtime} and status = #{completed}")
    Double getTurnover(LocalDateTime begintime, LocalDateTime endtime, Integer completed);

    /**
     * 获取订单总数
     * @param begintime
     * @param endtime
     * @return
     */
    @Select("select count(*) from orders where order_time between #{begintime} and #{endtime}")
    Integer getall(LocalDateTime begintime, LocalDateTime endtime);

    /**
     * 获取有效订单数
     * @param begintime
     * @param endtime
     * @param completed
     * @return
     */
    @Select("select count(*) from orders where order_time between #{begintime} and #{endtime} and status = #{completed}")
    Integer getvalid(LocalDateTime begintime, LocalDateTime endtime, Integer completed);
    @Select("select od.name,sum(od.number) numbers from order_detail od,orders o where o.id = od.order_id and o.status = #{completed} and o.order_time between #{begintime} and #{endtime} group by od.name order by numbers desc")
    List<GoodsSalesDTO> top10(LocalDateTime begintime, LocalDateTime endtime, Integer completed);


    /**
     * 动态获取订单数量
     * @param map
     * @return
     */
    @Select("select count(*) from orders where status = #{status} and order_time >#{begin}")
    Integer countByMap(Map map);
}
